package config

import (
	"fmt"
	"longmen/server/config/global"
	"net/url"

	"github.com/robfig/cron/v3"
)

type JobFunc func()
type Task struct {
	Spec   string       // 定时任务的spec
	Fun    JobFunc      // 任务的具体执行方法
	CronId cron.EntryID //  存储所有任务ID用的
}

var Nami namiConf

// NamiConf 纳米配置
type namiConf struct {
	User           string // 用户
	Key            string // 密钥
	Domain         string // 主域名
	ESportDomain   string // 主域名
	VideoDomain    string // 视频域名
	MQTT           mqttConf
	Resty          resty
	AutoCreateRoom int

	/* 是否 拉取 特定 运动项目 数据, 0 -> 否, 1 -> 是, */
	PullCricket    int
	PullFootball   int
	PullBasketball int
	PullLive       int
}

type resty struct {
	ProxyType string // 协议类型 url tcp socks http
	ProxyUrl  string // 链接
	ProxyUser string
	ProxyPass string
}

type mqttConf struct {
	Protocol string // 协议
	Broker   string // 集群地址
	Port     int    // 端口号
}

func InitConfig() {
	Nami.User = global.Config.GetString("nami.user")
	Nami.Key = global.Config.GetString("nami.key")
	Nami.MQTT.Broker = global.Config.GetString("nami.mqtt.broker")
	Nami.MQTT.Port = global.Config.GetInt("nami.mqtt.broker_port")
	Nami.MQTT.Protocol = global.Config.GetString("nami.mqtt.broker_protocol")
	Nami.Domain = global.Config.GetString("nami.domain")
	Nami.VideoDomain = global.Config.GetString("nami.video_domain")
	Nami.ESportDomain = global.Config.GetString("nami.esport_domain")
	Nami.Resty.ProxyType = global.Config.GetString("nami.resty.type")
	Nami.Resty.ProxyUrl = global.Config.GetString("nami.resty.url")
	Nami.Resty.ProxyUser = global.Config.GetString("nami.resty.proxy_user")
	Nami.Resty.ProxyPass = global.Config.GetString("nami.resty.proxy_pass")
	Nami.AutoCreateRoom = global.Config.GetInt("nami.tx_chat.auto_create=0")
	Nami.PullCricket = global.Config.GetInt("nami.pull_cricket")
	Nami.PullBasketball = global.Config.GetInt("nami.pull_football")
	Nami.PullFootball = global.Config.GetInt("nami.pull_basketball")
	Nami.PullLive = global.Config.GetInt("nami.pull_live")

	//nami.resty.type=socks5
	//nami.resty.url=127.0.0.1:1089
	//nami.resty.proxy_username=
	//nami.resty.proxy_password=

}

func NamiCricketEnabled() bool {
	return Nami.PullCricket == 1
}

func NamiBasketballEnabled() bool {
	return Nami.PullBasketball == 1
}

func NamiFootballEnabled() bool {
	return Nami.PullFootball == 1
}

func NamiLiveEnabled() bool {
	return Nami.PullLive == 1
}

const (
	ESportLiveUrl = "/sports_pushurl" // 电竞直播列表
	VideoLiveUrl  = "/pushurl_v4"     //视频直播列表
)

// 篮球的API接口连接 BasketBall 简写为 BB
const (
	BBCategoryUrl    = "/api/v5/basketball/category/list"            // 获取分类列表
	BBCountryUrl     = "/api/v5/basketball/country/list"             // 获取国家列表
	BBCompetitionUrl = "/api/v5/basketball/competition/list"         // 获取赛事列表
	BBTeamsUrl       = "/api/v5/basketball/team/list"                // 获取球队列表
	BBMatchDiaryUrl  = "/api/v5/basketball/match/schedule/diary"     // 获取赛程赛果列表-日期查询
	BBDelete         = "/api/v5/basketball/deleted"                  // 获取删除数据
	BBRecentChange   = "/api/v5/basketball/recent/match/list"        // 比赛变动接口
	BBOddsLive       = "/api/v5/basketball/odds/models_live"         // 实时指数数据
	BBOddsHistory    = "/api/v5/basketball/odds/history"             // 获取单场比赛指数数据
	BBCompPoint      = "/api/v5/basketball/competition/table/detail" // 赛事积分榜
	// 篮球阵容在MQTT获取

)

// 足球的API接口链接 FootBall 简写为 FB
const (
	FBCategoryUrl    = "/api/v5/football/category/list"            // FBCategoryUrl 获取分类列表
	FBCountryUrl     = "/api/v5/football/country/list"             // FBCountryUrl 获取国家列表
	FBCompetitionUrl = "/api/v5/football/competition/list"         // FBCompetitionUrl 获取赛事列表
	FBTeamsUrl       = "/api/v5/football/team/list"                // FBTeamsUrl 获取球队列表
	FBMatchDiaryUrl  = "/api/v5/football/match/schedule/diary"     // FBMatchDiaryUrl 获取赛程赛果列表-日期查询
	FBMatchParamUrl  = "/api/v5/football/match/schedule/param"     // FBMatchParamUrl 获取赛程赛果列表-维度查询
	FBDelete         = "/api/v5/football/deleted"                  // FBDelete 获取删除数据
	FBRecentChange   = "/api/v5/football/recent/match/list"        // FBRecentChange 比赛变动接口
	FBLineUp         = "/api/v5/football/match/lineup/detail"      // FBLineUp 足球阵容
	FBOddsLive       = "/api/v5/football/odds/models_live"         // FBOddsLive 足球实时指数数据
	FBOddsHistory    = "/api/v5/football/odds/history"             // FBOddsHistory 获取单场比赛指数数据
	FBVenue          = "/api/v5/football/venue/list"               // FBVenue 获取场馆列表
	FBEurCompanyList = "/api/v5/football/odds/europe/company/list" // FBOddsHistory 获取百欧公司列表
	FBEurOddsLive    = "/api/v5/football/odds/europe/models_live"  // FBEurOddsLive 获取实时百欧指数数据
	FBEurOddsHistory = "/api/v5/football/odds/europe/history"      // FBEurOddsLive 获取实时百欧指数数据
	FBCompPoint      = "/api/v5/football/competition/table/detail" // 赛事积分榜

)

// 板球的API接口链接 Cricket 简写为 CR
const (
	CRUrlPrefix           = "/api/v1/cricket"                       // Cricket api 前缀.
	CRCategoryUrl         = CRUrlPrefix + "/category/list"          // CRCategoryUrl 获取分类列表
	CRCountryUrl          = CRUrlPrefix + "/country/list"           // CRCountryUrl 获取国家列表
	CRUniqueTournamentUrl = CRUrlPrefix + "/unique_tournament/list" // CRUniqueTournamentUrl 获取赛事列表
	CRTeamsUrl            = CRUrlPrefix + "/team/list"              // CRTeamsUrl 获取球队列表
	CRMatchDiaryUrl       = CRUrlPrefix + "/match/diary"            // CRMatchDiaryUrl 获取赛程赛果列表-日期查询
	CRDelete              = CRUrlPrefix + "/deleted"                // CRDelete 获取删除数据
	CRLineUp              = CRUrlPrefix + "/match/lineup/detail"    // CRLineUp 板球阵容
	CRVenue               = CRUrlPrefix + "/venue/list"             // CRVenue 获取场馆列表
	CRCompPoint           = CRUrlPrefix + "/season/table/detail"    // 赛事积分榜
)

const (
	LOLMatchUrl      = "/v1/lol/diary/match"    // LOLMatch 获取赛程赛果列表-日期查询
	LOLDeleteUrl     = "/v1/lol/deleted/detail" // LOLDeleteUrl 获取删除数据
	LOLTournamentUrl = "/v1/lol/tournament"     // LOLTournamentUrl 获取赛事列表
	LOLTeamUrl       = "/v1/lol/team"           // LOLTeamUrl 获取战队列表
)

const (
	CSGOMatchUrl      = "/v1/csgo/diary/match"    // CSGOMatch 获取赛程赛果列表-日期查询
	CSGODeleteUrl     = "/v1/csgo/deleted/detail" // CSGODeleteUrl 获取删除数据
	CSGOTournamentUrl = "/v1/csgo/tournament"     // CSGOTournamentUrl 获取赛事列表
	CSGOTeamUrl       = "/v1/csgo/team"           // CSGOTeamUrl 获取战队列表
)

const (
	DOTAMatchUrl      = "/v1/dota2/diary/match"    // DOTAMatch 获取赛程赛果列表-日期查询
	DOTADeleteUrl     = "/v1/dota2/deleted/detail" // DOTADeleteUrl 获取删除数据
	DOTATournamentUrl = "/v1/dota2/tournament"     // DOTATournamentUrl 获取赛事列表
	DOTATeamUrl       = "/v1/dota2/team"           // DOTATeamUrl 获取战队列表
)

const (
	KOGMatchUrl      = "/v1/kog/diary/match"    // KOGMatch 获取赛程赛果列表-日期查询
	KOGDeleteUrl     = "/v1/kog/deleted/detail" // KOGDeleteUrl 获取删除数据
	KOGTournamentUrl = "/v1/kog/tournament"     // KOGTournamentUrl 获取赛事列表
	KOGTeamUrl       = "/v1/kog/team"           // KOGTeamUrl 获取战队列表
)

// BuildParamsAndUserAndSecret 构建参数，并把固定的签名参数返回
func BuildParamsAndUserAndSecret(args ...map[string]interface{}) string {
	var uri url.URL
	query := uri.Query()
	query.Add("user", Nami.User)
	query.Add("secret", Nami.Key)
	for _, arg := range args {
		for k, v := range arg {
			query.Add(k, fmt.Sprintf("%v", v))
		}
	}
	return "?" + query.Encode()
}
